﻿
import Module = Sustainalytics.Module;


require.config({
    paths:
    {
        "Scripts/libs/es5-shim": ["Scripts/libs/es5-shim.min"],

        "jquery": ["Scripts/libs/jquery-1.9.0.min"],
        "Scripts/libs/jquery": ["Scripts/libs/jquery-1.9.0.min"],
        "Scripts/libs/jquery-ui": ["Scripts/libs/jquery-ui-1.10.4.min"],
        "Scripts/libs/jquery.signalR": ["Scripts/libs/jquery.signalR-2.2.0.min"],
        "Scripts/libs/jquery-ui-slider-pips": ["Scripts/libs/jquery-ui-slider-pips"],

        "Scripts/libs/angular-1.2/angular": ["Scripts/libs/angular-1.2/angular"],
        "Scripts/libs/angular-1.2/angular-route": ["Scripts/libs/angular-1.2/angular-route"],
        "Scripts/libs/angular-1.2/angular-animate": ["Scripts/libs/angular-1.2/angular-animate"],
        "Scripts/libs/angular-1.2/angular-ng-grid": ["Scripts/libs/angular-1.2/angular-ng-grid"],
        "Scripts/libs/angular-1.2/angular-resource": ["Scripts/libs/angular-1.2/angular-resource"],
        "Scripts/libs/angular-1.2/angular-sanitize": ["Scripts/libs/angular-1.2/angular-sanitize"],
        "Scripts/libs/angular-1.2/angular-ui": ["Scripts/libs/angular-1.2/angular-ui"],
        "Scripts/libs/angular-1.2/angular-file-upload-shim": ["Scripts/libs/angular-1.2/angular-file-upload-shim"],
        "Scripts/libs/angular-1.2/angular-file-upload": ["Scripts/libs/angular-1.2/angular-file-upload"],

        "Scripts/libs/angular-last/angular": ["Scripts/libs/angular-last/angular"],
        "Scripts/libs/angular-last/angular-route": ["Scripts/libs/angular-last/angular-route"],
        "Scripts/libs/angular-last/angular-animate": ["Scripts/libs/angular-last/angular-animate"],
        "Scripts/libs/angular-last/angular-ng-grid": ["Scripts/libs/angular-last/angular-ng-grid"],
        "Scripts/libs/angular-last/angular-resource": ["Scripts/libs/angular-last/angular-resource"],
        "Scripts/libs/angular-last/angular-sanitize": ["Scripts/libs/angular-last/angular-sanitize"],
        "Scripts/libs/angular-last/angular-ui": ["Scripts/libs/angular-last/angular-ui"],
        "Scripts/libs/angular-last/angular-file-upload-shim": ["Scripts/libs/angular-last/angular-file-upload-shim"],
        "Scripts/libs/angular-last/angular-file-upload": ["Scripts/libs/angular-last/angular-file-upload"],

        "Scripts/libs/moment": ["Scripts/libs/moment"],
        "Scripts/libs/sortable": ["Scripts/libs/sortable"],
        "Scripts/libs/ui-bootstrap-tpls": ["Scripts/libs/ui-bootstrap-tpls"],
        "Scripts/libs/chart": ["Scripts/libs/chart"],
        "Scripts/libs/select2": ["Scripts/libs/select2"],

        "amcharts": ["Scripts/libs/amcharts"],
        "amcharts.themes.light": ["Scripts/libs/light"],
        "amcharts.serial": ["Scripts/libs/serial"],
        "amcharts.xy": ["Scripts/libs/xy"]
    },
    // mure
    shim:
    {
        // require jquery.signalR before obtaining signalR proxies
        'Scripts/libs/jquery.signalR': ['modules/Module/ModuleLoader!modules/_Libs/jQuery/module.json'],
        '/lrtmserv/signalr/hubs': ['Scripts/libs/jquery.signalR'],
        '/screeningtoolserv/signalr/hubs': ['Scripts/libs/jquery.signalR'],
        '/accountsserv/signalr/hubs': ['Scripts/libs/jquery.signalR'],


        'Scripts/libs/toastr':
        {
            deps: ['jquery'],
            exports: 'toastr'
        },

        'amcharts.themes.light':
        {
            deps: ['amcharts'],
            exports: 'AmCharts',
            init: function () {
                window['AmCharts']['isReady'] = true;
            }
        },

        'amcharts.serial':
        {
            deps: ['amcharts', 'amcharts.themes.light'],
            exports: 'AmCharts',
            init: function () {
                window['AmCharts']['isReady'] = true;
            }
        },

        'amcharts.xy':
        {
            deps: ['amcharts', 'amcharts.themes.light'],
            exports: 'AmCharts',
            init: function () {
                window['AmCharts']['isReady'] = true;
            }
        }
    },
    urlArgs: "bust=v2"
});


require(['jquery', 'Scripts/libs/es5-shim'], function ($) {

    Module.ModuleLoaderConfig.current.useBundles = true;

    // load modules and bundles
    var loadDef = Module.ModuleLoaderConfig.current.loadFromJson(
        'modules/bundles/modules.json',
        'modules/bundles/bundles.json');

    // handle progress
    Module.ModuleLoaderConfig.current.subscribeToProgress((doneCount, totalCount) => {

        var perc = 100;

        if (totalCount > 0) {
            perc = Math.round(doneCount / totalCount * 100);
        }

        $('#moduleLoaderProgress-ratio').width(perc + '%');

        if (perc == 100) {
            $('#moduleLoaderProgress').removeClass('visible');
        }
        else {
            $('#moduleLoaderProgress').addClass('visible');
        }
    });

    loadDef.fail(function () {
        console.error('start :: ModuleLoaderConfig :: Error loading config from JSON.');
    });

    // after configuration is complete load front module
    loadDef.done(function () {
    
        var loadModuleName = 'modules/Module/ModuleLoader!modules/bundles/ShellModule.Admin.min';

        // load startup modules
        require([loadModuleName],
            // success
            function () {
            },
            // error
            function () {
                console.error('Error loading ' + loadModuleName);
            });
    });
});